/**
 * @class common-boots.js
 * @author jiagui E-mail:<zhujiagui@zkingsoft.com>
 * @date 2015年7月10日 下午5:03:33
 * @description 基于bootstrap样式，对常用控件数据的操作进行封装
 */
(function($) {
	function BootTools() {
	}
	$
			.extend(
					BootTools.prototype,
					{
						loadSelectData : function() {
							$(".bootselect")
									.each(
											function(index) {
												var htmls = "";
												var _this = $(this);
												var datavalue = null;
												var emptyText = _this
														.attr("data-placeholder");
												var multiple = _this
														.attr("multiple");
												if (emptyText == null
														|| emptyText == undefined
														|| emptyText == "") {
													emptyText = "--请选择数据--";
												}
												if (multiple == null
														|| multiple == undefined) {
													htmls += "<option selected='selected' value=''>"
															+ emptyText
															+ "</option>";
												}
												if (_this.attr("url") != null) {
													$
															.ajax({
																cache : true,
																async : false,
																url : _this
																		.attr("url"),
																success : function(
																		data) {
																	if (data != null) {
																		var dataList = null;
																		if (typeof data == "object"
																				&& data.code != null
																				&& data.code == "true") {// 查询数据成功，且数据不为空
																			if (_this
																					.attr("dataField") != null
																					&& data.result != null
																					&& data.result[_this
																							.attr("dataField")] != null) {
																				dataList = data.result[_this
																						.attr("dataField")];
																			}
																		} else if (typeof data == "string") {
																			dataList = eval(data);// 将json数组转化为数组对象
																		} else {
																			dataList = data;
																		}
																		if (dataList != null) {
																			var content = "";
																			for (var i = 0, a = dataList.length; i < a; i++) {
																				htmls += "<option";
																				for ( var fname in dataList[i]) {
																					datavalue = dataList[i][fname];
																					if (datavalue != null) {
																						htmls += " "
																								+ fname
																								+ "='"
																								+ dataList[i][fname]
																										.toString()
																										.trim()
																								+ "'";
																					}
																					if (datavalue != null
																							&& fname == _this
																									.attr("valueField")) {
																						htmls += " value='"
																								+ datavalue
																										.toString()
																								+ "'";
																					}
																					if (datavalue != null
																							&& fname == _this
																									.attr("textField")) {
																						content = datavalue
																								.toString();
																					}
																				}
																				htmls += ">"
																						+ content
																						+ "</option>";
																			}
																		}
																	}
																}
															});
												}
												_this.html(htmls.toString());
											});
						},
						loadingshow : function(msg, time, showMask) {// 保存数据
							if (showMask == null || showMask) {
								if ($(".msgDialog_back").length > 0) {
									$(".msgDialog_back").show();// 显示加载提示框
								} else {
									var sHeight = document.body.clientHeight > window.screen.availHeight ? document.body.clientHeight
											: window.screen.availHeight;
									var div = document.createElement("div");// 遮罩层
									div.setAttribute("class", "msgDialog_back");
									div.style.width = "100%";
									div.style.height = sHeight + "px";
									div.style.position = "fixed";
									div.style.top = "0px";
									div.style.left = "0px";
									div.style.zIndex = 1000;
									div.style.backgroundColor = "#777"
									div.style.filter = "Alpha(opacity=60)";
									div.style.opacity = "0.6";
									document.body.appendChild(div);
								}
							}
							if ($(".msgDialog").length > 0) {
								$("#msgDialog_msg").html(msg);
								$(".msgDialog").show();// 显示加载提示框
							} else {
								var msgDialog = "<div class='msgDialog' style='display:black;position:fixed;left:40%;z-index: 1001;";
								msgDialog += "top:40%;border:2px solid #48A448;width:auto;background-color:#F5F5F5;color:#3276B1;";
								msgDialog += "font-size:20px;font-style:bold;font-family:微软雅黑; '><i class='icon-spinner icon-spin'></i>";
								msgDialog += "<span id='msgDialog_msg'>" + msg
										+ "</span></div>";
								$("body").append(msgDialog);// 添加加载提示框
							}
							if (time != null && !isNaN(time)) {
								setTimeout(function() {// 自动隐藏
									$btools.loadinghide();// 隐藏加载提示框
								}, time);
							}
						},
						loadinghide : function() {
							$(".msgDialog_back").hide();// 隐藏遮罩层
							$(".msgDialog").hide();// 隐藏加载提示框
						},
						blockUI : function(options) {// 显示遮罩层
							var defaults = {
								target : null,// 锁定目标
								overlayColor : 'black',// 背景颜色
								cenrerY : true,
								animate : false,
								boxed : false,// 消息边框
								iconOnly : false,// 只显示图标
								textOnly : false,// 只显示文字
								msg : "Loading...",// 显示消息
								time : null
							// 显示时间
							}
							var opts = $.extend(defaults, options);
							Metronic.blockUI({
								target : opts.target,
								boxed : opts.boxed,
								message : opts.msg,
								overlayColor : opts.overlayColor,
								cenrerY : opts.cenrerY,
								animate : opts.animate,
								iconOnly : opts.iconOnly,
								textOnly : opts.textOnly
							});
							if (opts.time != null && !isNaN(opts.time)) {
								window.setTimeout(function() {
									Metronic.unblockUI();
								}, opts.time);
							}
						},
						unblockUI : function() {
							Metronic.unblockUI();// 关闭遮罩层
						},
						prompt : function(msg, onok, oncancel) {
							var modaldiv = document.createElement("div");
							modaldiv.setAttribute("class",
									"modal fade bs-example-modal-sm");
							var dialogdiv = document.createElement("div");
							dialogdiv.setAttribute("class",
									'modal-dialog modal-sm');
							var contentdiv = document.createElement("div");
							contentdiv.setAttribute("class", "modal-content");
							var headerdiv = document.createElement("div");
							headerdiv.setAttribute("class", "modal-header");
							var titleh4 = document.createElement("h4");
							titleh4.setAttribute("class", "modal-title");
							var iconspan = "<span class='icon-question-sign' aria-hidden='true'></span>&nbsp;温馨提示：";
							titleh4.innerHTML = iconspan;
							headerdiv.appendChild(titleh4);
							var bodydiv = document.createElement("div");
							bodydiv.setAttribute("class", "modal-body");
							var msgp = document.createElement("p");
							msgp.innerHTML = msg;
							bodydiv.appendChild(msgp);
							var footerdiv = document.createElement("div");
							footerdiv.setAttribute("class", "modal-footer");
							var cancelBtn = document.createElement("button");
							cancelBtn.setAttribute("class", "btn btn-default");
							cancelBtn.setAttribute("type", "button");
							cancelBtn.innerHTML = "取消";
							cancelBtn.onclick = function(e) {// 保存数据
								e.preventDefault();
								$(modaldiv).modal("hide");
								if (oncancel != null) {
									oncancel();// 回调函数
								}
								setTimeout(
										function() {
											var modal_backdrop = $(modaldiv)
													.next("div");
											if (modal_backdrop != null
													&& modal_backdrop != undefined) {
												if (modal_backdrop
														.attr("class") != undefined
														&& modal_backdrop
																.attr("class")
																.indexOf(
																		"modal-backdrop") >= 0) {
													modal_backdrop.remove();// 可能存在模态框还未完全隐藏就调用了此方法，此时需将遮罩层一起删除
												}
											}
											$(modaldiv).remove();
										}, 600);
							}
							var okBtn = document.createElement("button");
							okBtn.setAttribute("class", "btn btn-primary");
							okBtn.setAttribute("type", "button");
							okBtn.innerHTML = "确认";
							okBtn.onclick = function(e) {// 保存数据
								e.preventDefault();
								$(modaldiv).modal("hide");
								if (onok != null) {
									onok();// 回调函数
								}
								setTimeout(
										function() {
											var modal_backdrop = $(modaldiv)
													.next("div");
											if (modal_backdrop != null
													&& modal_backdrop != undefined) {
												if (modal_backdrop
														.attr("class") != undefined
														&& modal_backdrop
																.attr("class")
																.indexOf(
																		"modal-backdrop") >= 0) {
													modal_backdrop.remove();// 可能存在模态框还未完全隐藏就调用了此方法，此时需将遮罩层一起删除
												}
											}
											$(modaldiv).remove();
										}, 600);
							}
							footerdiv.appendChild(cancelBtn);
							footerdiv.appendChild(okBtn);
							contentdiv.appendChild(headerdiv);
							contentdiv.appendChild(bodydiv);
							contentdiv.appendChild(footerdiv);
							dialogdiv.appendChild(contentdiv);
							modaldiv.appendChild(dialogdiv);
							document.body.appendChild(modaldiv);
							$(modaldiv).modal("show");
						},
						alert : function(msg, callback) {
							var modaldiv = document.createElement("div");
							modaldiv.setAttribute("class",
									"modal fade bs-example-modal-sm");
							var dialogdiv = document.createElement("div");
							dialogdiv.setAttribute("class",
									'modal-dialog modal-sm');
							var contentdiv = document.createElement("div");
							contentdiv.setAttribute("class", "modal-content");
							var headerdiv = document.createElement("div");
							headerdiv.setAttribute("class", "modal-header");
							var titleh4 = document.createElement("h4");
							titleh4.setAttribute("class", "modal-title");
							var iconspan = "<span class='icon-info-sign' aria-hidden='true'></span>&nbsp;温馨提示：";
							titleh4.innerHTML = iconspan;
							headerdiv.appendChild(titleh4);
							var bodydiv = document.createElement("div");
							bodydiv.setAttribute("class", "modal-body");
							var msgp = document.createElement("p");
							msgp.innerHTML = msg;
							bodydiv.appendChild(msgp);
							var footerdiv = document.createElement("div");
							footerdiv.setAttribute("class", "modal-footer");
							var okBtn = document.createElement("button");
							okBtn.setAttribute("class", "btn btn-primary");
							okBtn.setAttribute("type", "button");
							okBtn.innerHTML = "确认";
							okBtn.onclick = function(e) {// 保存数据
								e.preventDefault();
								$(modaldiv).modal("hide");
								if (callback != null) {
									callback();// 回调函数
								}
								setTimeout(function() {
									$(modaldiv).remove();
								}, 600);
							}
							footerdiv.appendChild(okBtn);
							contentdiv.appendChild(headerdiv);
							contentdiv.appendChild(bodydiv);
							contentdiv.appendChild(footerdiv);
							dialogdiv.appendChild(contentdiv);
							modaldiv.appendChild(dialogdiv);
							document.body.appendChild(modaldiv);
							$(modaldiv).modal("show");
						},
						insertContent : function(options) {
							var defaults = {
								modalCls : "modal fade",// 模态框样式
								width : null,// 模态框宽度
								dialogCls : "modal-dialog modal-sm",// dialog样式(String)
								contentCls : "modal-content",// content样式
								headerCls : "modal-header",// 头部样式
								titleCls : "modal-title",// 标题样式
								bodyCls : "modal-body",// 主体样式
								iframeCls : "embed-responsive-item",// iframe样式
								footerCls : "modal-footer",// 底部样式
								showCloseBtn : true,// 是否显示右上角关闭小按钮
								title : "内容插入",// 模态框标题
								iconCls : "",// 模态框图标样式
								bodyCls : "embed-responsive embed-responsive-16by9",// 模态框body样式
								dismissBtnCls : "btn btn-default",// 取消按钮样式
								dismissBtnText : "取消",// 取消按钮文字
								saveBtnCls : "btn btn-primary",// 确定按钮文字
								okBtnText : "确定",// 确定按钮文字
								closeOnOkBtnclick : true,// 点击确定按钮时是否关闭模态框
								onOkBtnclick : null
							// 保存按钮点击事件
							}
							var opts = $.extend(defaults, options);
							var modaldiv = document.createElement("div");
							opts.modal = $(modaldiv);
							modaldiv.setAttribute("class", opts.modalCls);
							if (opts.modalId != null) {
								modaldiv.setAttribute("id", opts.modalId);
							}
							var dialogdiv = document.createElement("div");
							dialogdiv.setAttribute("class", opts.dialogCls);
							if (opts.width != null && opts.width != "") {
								dialogdiv.style.width = opts.width;
							}
							var contentdiv = document.createElement("div");
							contentdiv.setAttribute("class", opts.contentCls);
							var headerdiv = document.createElement("div");
							headerdiv.setAttribute("class", opts.headerCls);
							if (opts.showCloseBtn) {
								var closeBtn = document.createElement("button");
								closeBtn.setAttribute("class", "close");
								closeBtn.setAttribute("type", "button");
								closeBtn.innerHTML = "<span aria-hidden='true'>&times;</span>";
								closeBtn.onclick = function(e) {
									e.preventDefault();
									$btools.destroyModal(opts);
								}
								headerdiv.appendChild(closeBtn);
							}
							var titleh4 = document.createElement("h4");
							titleh4.setAttribute("class", opts.titleCls);
							var iconspan = "";
							if (opts.iconCls != null && opts.iconCls != "") {
								iconspan += "<span class='" + opts.iconCls
										+ "' aria-hidden='true'></span>&nbsp";
							}
							titleh4.innerHTML = iconspan + opts.title;
							headerdiv.appendChild(titleh4);
							contentdiv.appendChild(headerdiv);
							var bodydiv = document.createElement("div");
							bodydiv.setAttribute("class", opts.bodyCls);
							var rowdiv = document.createElement("div");
							rowdiv.setAttribute("class", "row");
							var textarea = document.createElement("textarea");
							textarea.setAttribute("placeholder", "请输入内容");
							textarea.setAttribute("rows", 4);
							textarea.style.backgroundColor = "#fff";
							textarea.style.border = "1px solid #e5e5e5";
							textarea.style.borderRadius = "4px";
							textarea.style.width = "90%";
							textarea.style.fontSize = "18px";
							textarea.style.marginTop = "8%";
							textarea.style.marginLeft = "5%";
							var subrowdiv = document.createElement("div");
							subrowdiv.setAttribute("class", "col-xs-12");
							var groupdiv = document.createElement("div");
							groupdiv.setAttribute("class", "form-group");
							groupdiv.appendChild(textarea);
							subrowdiv.appendChild(groupdiv);
							rowdiv.appendChild(subrowdiv);
							bodydiv.appendChild(rowdiv);
							contentdiv.appendChild(bodydiv);
							var footerdiv = document.createElement("div");
							footerdiv.setAttribute("class", opts.footerCls);
							var dismissBtn = document.createElement("button");
							dismissBtn
									.setAttribute("class", opts.dismissBtnCls);
							dismissBtn.setAttribute("type", "button");
							dismissBtn.innerHTML = opts.dismissBtnText;
							dismissBtn.onclick = function(e) {
								e.preventDefault();
								$btools.destroyModal(opts);
							};
							footerdiv.appendChild(dismissBtn);
							var okBtn = document.createElement("button");
							okBtn.setAttribute("class", opts.saveBtnCls);
							okBtn.setAttribute("type", "button");
							okBtn.innerHTML = opts.okBtnText;
							if (opts.onOkBtnclick != null
									&& opts.onOkBtnclick != undefined) {
								okBtn.onclick = function(e) {
									e.preventDefault();
									opts.content = textarea.value;
									if (!opts.content) {
										$btools.alert("请输入内容！");
										return;
									}
									if (opts.closeOnOkBtnclick) {
										$btools.destroyModal(opts);
									}
									eval(opts.onOkBtnclick(opts));
								}
							} else {
								okBtn.onclick = function(e) {// 保存数据
									e.preventDefault();
									opts.content = textarea.value;
									if (!opts.content) {
										$btools.alert("请输入内容！");
										return;
									}
									$btools.destroyModal(opts);
								}
							}
							footerdiv.appendChild(okBtn);
							contentdiv.appendChild(footerdiv);
							dialogdiv.appendChild(contentdiv);
							modaldiv.appendChild(dialogdiv);
							document.body.appendChild(modaldiv);
							$(modaldiv).modal("show");
						},
						openUrlInModal : function(options) {
							var defaults = {
								url : "",// 资源加载地址
								init : true,
								action : "add",// 动作标识
								modalCls : "modal fade",// 模态框样式
								modalId : null,// 模态框id
								iframeId : null,// 模态框内的fram的唯一标识
								width : null,// 模态框宽度
								dialogCls : "modal-dialog",// dialog样式(String)
								contentCls : "modal-content",// content样式
								headerCls : "modal-header",// 头部样式
								titleCls : "modal-title",// 标题样式
								bodyCls : "modal-body",// 主体样式
								iframeCls : "embed-responsive-item",// iframe样式
								footerCls : "modal-footer",// 底部样式
								showCloseBtn : true,// 是否显示右上角关闭小按钮
								title : "对话框",// 模态框标题
								iconCls : "",// 模态框图标样式
								bodyCls : "embed-responsive embed-responsive-16by9",// 模态框body样式
								innerBodyHTML : null,// 模态框body内显示的内容
								showFooter : true,// 是否显示底部
								footBtnList : null,// 模态框底部按钮组
								showDismissBtn : true,// 是否显示取消按钮
								dismissBtnCls : "btn btn-default",// 取消按钮样式
								dismissBtnText : "取消",// 取消按钮文字
								showSaveBtn : true,// 是否显示确定按钮
								saveBtnCls : "btn btn-primary",// 确定按钮文字
								saveBtnText : "保存",// 确定按钮文字
								closeOnSaveBtnclick : true,// 点击确定按钮时是否关闭模态框
								onSaveBtnclick : null,// 保存按钮点击事件
								subformId : null,// 子页面中form的id
								onSaveSuccess : null,// 保存按钮保存数据成功时调用
								backdrop : 'static',// backdrop:static时,空白处不关闭.
								keyboard : false,// keyboard:false时,esc键盘不关闭.
								oncreated : null,// 模态框创建完时执行
								onload : null,// ifram元素加载完时执行
								ondataload : null,// 数据加载完成时执行
								beforedestroy : null,// 点击关闭按钮模态框销毁之前执行
								ondestroyed : null
							// 模态框销毁之后执行
							};
							var opts = $.extend(defaults, options);
							var modaldiv = document.createElement("div");
							opts.modal = $(modaldiv);
							modaldiv.setAttribute("class", opts.modalCls);
							if (opts.modalId != null) {
								modaldiv.setAttribute("id", opts.modalId);
							}
							var dialogdiv = document.createElement("div");
							dialogdiv.setAttribute("class", opts.dialogCls);
							if (opts.width != null && opts.width != "") {
								dialogdiv.style.width = opts.width;
							}
							var contentdiv = document.createElement("div");
							contentdiv.setAttribute("class", opts.contentCls);
							var headerdiv = document.createElement("div");
							headerdiv.setAttribute("class", opts.headerCls);
							var bodyiframe = document.createElement("iframe");
							if (opts.showCloseBtn) {
								var closeBtn = document.createElement("button");
								closeBtn.setAttribute("class", "close");
								closeBtn.setAttribute("type", "button");
								closeBtn.innerHTML = "<span aria-hidden='true'>&times;</span>";
								closeBtn.onclick = function(e) {
									e.preventDefault();
									if (bodyiframe.contentWindow.$&&bodyiframe.contentWindow.$.form) {
										if (bodyiframe.contentWindow.$.form
												.getchangedProperties().length > 0) {
											$btools
													.prompt(
															"表单数据已被修改，您确定不保存关闭吗？",
															function() {
																$btools
																		.destroyModal(opts);
															});
										} else {
											$btools.destroyModal(opts);
										}
									} else {
										$btools.destroyModal(opts);
									}
								}
								headerdiv.appendChild(closeBtn);
							}
							var titleh4 = document.createElement("h4");
							titleh4.setAttribute("class", opts.titleCls);
							var iconspan = "";
							if (opts.iconCls != null && opts.iconCls != "") {
								iconspan += "<span class='" + opts.iconCls
										+ "' aria-hidden='true'></span>&nbsp";
							}
							titleh4.innerHTML = iconspan + opts.title;
							headerdiv.appendChild(titleh4);
							contentdiv.appendChild(headerdiv);
							var bodydiv = document.createElement("div");
							bodydiv.setAttribute("class", opts.bodyCls);
							var bodysubdiv = document.createElement("div");
							bodysubdiv.setAttribute("class", opts.bodyCls);
							if (opts.innerBodyHTML != null) {
								bodydiv.innerHTML = opts.innerBodyHTML;
							}
							bodyiframe.setAttribute("id", opts.iframeId);
							bodyiframe.setAttribute("class", opts.iframeCls);
							bodyiframe.setAttribute("src", opts.url);
							opts.iframe = bodyiframe;
							var form = null;
							bodyiframe.onload = function(e) {// 加载完成
								form = $btools.getForm(opts);// 获取表单
								opts.form = $(form);
								if (opts.onload != null
										&& opts.onload != undefined) {
									opts.onload(opts);
								}
								if (form != null && form != undefined) {
									var btnhtml = "<button type='submit' style='display: none;'></button>";
									$(form).append(btnhtml);
									if (opts.init
											&& bodyiframe.contentWindow.$&&bodyiframe.contentWindow.$.form) {
										bodyiframe.contentWindow.$.form
												.saveData({// 初始化表单验证保存方法
													form : $(form),
													beforvalid : null,// 表单验证之前
													beforsubmit : function(opt) {// 表单验证通过，提交之前
														$("#save_btn_single")
																.prop(
																		"disabled",
																		true);
													},
													onSaveSuccess : opts.onSaveSuccess,
													savesuccess : function(opt) {
														if (opt.msg != null
																&& opt.msg != "") {
															if (opt.code == "true") {// 保存成功
																var _datagrid = $(
																		e)
																		.closest(
																				".boot-dataGrid");
																$.grid
																		.search({
																			datagrid : $(_datagrid[0]),
																			action : opts.action
																		});
																$btools
																		.destroyModal(opts);
															} else {
																$(
																		"#save_btn_single")
																		.prop(
																				"disabled",
																				false);
																opt.form
																		.find(
																				"button[type='submit']")
																		.prop(
																				"disabled",
																				false);
															}
															$btools
																	.showToast({
																		msg : opt.msg,
																		shortCutFunction : opt.shortCutFunction,
																		positionClass : "toast-center-center"
																	});
														}
													}
												});
									}
								}
								if (opts.showFooter && opts.footBtnList != null
										&& opts.footBtnList.length > 0) {// 注册自定义按钮点击事件
									var targets = "";
									$(".selfBtn")
											.click(
													function() {
														targets = $(this).attr(
																"target")
																.split("_");
														var onBtnclick = opts.footBtnList[parseInt(targets[1])].onBtnclick;
														if (onBtnclick != null) {
															onBtnclick(opts);
														} else {
															$btools
																	.alert("请编写自己的点击事件！");
														}
													});
								}
							}
							bodysubdiv.appendChild(bodyiframe);
							bodydiv.appendChild(bodysubdiv);
							contentdiv.appendChild(bodydiv);
							if (opts.showFooter) {
								var footerdiv = document.createElement("div");
								footerdiv.setAttribute("class", opts.footerCls);
								if (opts.footBtnList != null
										&& opts.footBtnList.length > 0) {
									var selfBtn = null;
									for (var int = 0, a = opts.footBtnList.length; int < a; int++) {
										selfBtn = document
												.createElement("button");
										selfBtn.setAttribute("type", "button");
										selfBtn.setAttribute("target",
												"target_" + int);
										if (opts.footBtnList[int].btnCls != null) {
											selfBtn
													.setAttribute(
															"class",
															opts.footBtnList[int].btnCls
																	+ " selfBtn");
										} else {
											selfBtn.setAttribute("class",
													"btn btn-default selfBtn");
										}
										if (opts.footBtnList[int].btnText != null
												&& opts.footBtnList[int].btnText != "") {
											selfBtn.innerHTML = opts.footBtnList[int].btnText;
										} else {
											selfBtn.innerHTML = "Button" + int;
										}
										footerdiv.appendChild(selfBtn);
										var onBtnclick = opts.footBtnList[int].onBtnclick;
									}
								}
								if (opts.showDismissBtn) {
									var dismissBtn = document
											.createElement("button");
									dismissBtn.setAttribute("class",
											opts.dismissBtnCls);
									dismissBtn.setAttribute("type", "button");
									dismissBtn.innerHTML = opts.dismissBtnText;
									dismissBtn.onclick = function(e) {
										e.preventDefault();
										if (bodyiframe.contentWindow.$&&bodyiframe.contentWindow.$.form) {
											if (bodyiframe.contentWindow.$.form
													.getchangedProperties().length > 0) {
												$btools
														.prompt(
																"表单数据已被修改，您确定不保存关闭吗？",
																function() {
																	$btools
																			.destroyModal(opts);
																});
											} else {
												$btools.destroyModal(opts);
											}
										} else {
											$btools.destroyModal(opts);
										}
									};
									footerdiv.appendChild(dismissBtn);
								}
								if (opts.showSaveBtn) {
									var saveBtn = document
											.createElement("button");
									saveBtn.setAttribute("class",
											opts.saveBtnCls);
									saveBtn.setAttribute("type", "button");
									saveBtn.setAttribute("id",
											"save_btn_single");
									saveBtn.innerHTML = opts.saveBtnText;
									if (opts.onSaveBtnclick != null
											&& opts.onSaveBtnclick != undefined) {
										saveBtn.onclick = function(e) {
											e.preventDefault();
											if (opts.closeOnSaveBtnclick) {
												$btools.destroyModal(opts);
											}
											eval(opts.onSaveBtnclick(opts));
										}
									} else {
										saveBtn.onclick = function(e) {// 保存数据
											e.preventDefault();
											if (form == null
													|| form == undefined) {
												$btools.alert("该页面没有可提交的表单！");
												return;
											}
											if (bodyiframe.contentWindow.$&&bodyiframe.contentWindow.$.form) {
												bodyiframe.contentWindow.$.form
														.submitForm({
															form : $(form)
														});
											}
										}
									}
									footerdiv.appendChild(saveBtn);
								}
								contentdiv.appendChild(footerdiv);
							}
							dialogdiv.appendChild(contentdiv);
							modaldiv.appendChild(dialogdiv);
							document.body.appendChild(modaldiv);
							$(modaldiv).modal({
								backdrop : opts.backdrop,
								keyboard : opts.keyboard
							});
							if (opts.oncreated != null
									&& opts.oncreated != undefined) {
								opts.oncreated(opts);
							}
						},
						destroyModal : function(opts) {
							if (opts.beforedestroy != null
									&& opts.beforedestroy != undefined) {
								opts.beforedestroy(opts);
							}
							opts.modal.modal("hide");
							setTimeout(
									function() {
										var modal_backdrop = opts.modal
												.next("div");
										if (modal_backdrop != null
												&& modal_backdrop != undefined) {
											if (modal_backdrop.attr("class") != undefined
													&& modal_backdrop.attr(
															"class").indexOf(
															"modal-backdrop") >= 0) {
												modal_backdrop.remove();// 可能存在模态框还未完全隐藏就调用了此方法，此时需将遮罩层一起删除
											}
										}
										opts.modal.remove();
										if (opts.ondestroyed != null
												&& opts.ondestroyed != undefined) {
											opts.ondestroyed(opts);
										}
									}, 500);
						},
						getForm : function(opts) {// 获取iframe中的表单
							var form = null;
							var myiframe = opts.iframe;
							if ((myiframe == null || myiframe == undefined)
									&& opts.iframeId != null) {
								myiframe = document
										.getElementById(opts.iframeId);
							}
							if (opts.subformId != null) {
								form = $(myiframe.contentWindow.document.body)
										.find(
												"form[id='" + opts.subformId
														+ "']");
							} else {
								var forms = $(
										myiframe.contentWindow.document.body)
										.find("form");
								if (forms != null && forms.length > 0) {
									form = forms[0];
								}
							}
							return form;
						},
						submitData : function(options) {
							var defaults = {
								refrsh : false,// 数据提交成功之后是否刷新表格
								datagrid : null,// 数据提交成功之后要刷新的表格
								url : "",// 数据提交地址
								data : null,// 数据对象
								onError : null,
								onSuccess : null,
								onHandleSuccess : null
							};
							var opts = $.extend(defaults, options);
							if (opts.url != null && opts.url != "") {
								$btools.blockUI({
									msg : "正在提交数据，请稍候哦哦哦哦..."
								});
								$.ajax({
									cache : true,
									async : false,
									type : "POST",
									url : opts.url,
									data : opts.data,
									error : function(request) {
										if (opts.onError != null) {
											opts.request = request;
											opts.onError(opts);
										} else {
											$btools.alert("请求出错~", function() {
												$btools.unblockUI();// 隐藏加载提示框
											});
											return;
										}
									},
									success : function(data) {
										$btools.unblockUI();// 隐藏加载提示框
										opts.data = data;
										if (opts.onSuccess != null) {
											opts.onSuccess(opts);
										} else {
											$btools.dataHandle(opts);
											if (opts.onHandleSuccess != null) {
												opts.onHandleSuccess(opts);
											}
										}
									}
								});
							}
						},
						dataHandle : function(options) {// 数据处理函数
							var defaults = {
								data : null,// 要处理的数据对象
								datagrid : null,// 数据提交成功之后要刷新的表格
								refrsh : false,// 是否刷新
								msg : "msg",// 返回消息标识
								list : "list",// 返回数据集合标识
								obj : "obj"// 返回数据对象标识
							};
							var opts = $.extend(defaults, options);
							var msg = "操作失败！";
							var code = null;
							var data = opts.data;
							var shortCutFunction = "info";
							if (data != null) {
								code = data.code;
								var remsg = data.result["msg"];
								if (code == "true") {
									shortCutFunction = "success";
									if (remsg != null && remsg != undefined) {
										msg = remsg;
									} else {
										msg = "操作成功！";
									}
								} else if (code == "false") {
									shortCutFunction = "warning";
									if (remsg != null && remsg != undefined) {
										msg = remsg;
									} else {
										msg = "操作失败！";
									}
								} else if (code == "error") {
									shortCutFunction = "error";
									if (remsg != null && remsg != undefined) {
										msg = remsg;
									} else {
										msg = "操作异常！";
									}
								} else if (code == "info") {
									shortCutFunction = "info";
									if (remsg != null && remsg != undefined) {
										msg = remsg;
									} else {
										msg = "无需保存！";
									}
								}
							}
							if (opts.refrsh) {// 是否刷新表格
								$.grid.init({
									datagrid : opts.datagrid
								});// 刷新表格数据
							}
							$btools.showToast({
								msg : msg,
								shortCutFunction : shortCutFunction,
								positionClass : "toast-center-center"
							});
						},
						rendBootselect : function() {
							if ($(".bootselect").length > 0) {
								var _this = null;
								var _buttonWidth = null;
								var _selectedClass = null;
								var _includeSelectAllOption = null;
								var _selectAllText = null;
								var _onSelectAll = null;
								var _enableFiltering = null;
								$(".bootselect")
										.each(
												function() {
													_this = $(this);
													_buttonWidth = _this
															.attr("buttonWidth");
													_selectedClass = _this
															.attr("selectedClass");
													_includeSelectAllOption = _this
															.attr("selectAllOption");
													_selectAllText = _this
															.attr("selectAllText");
													_onSelectAll = _this
															.attr("onSelectAll");
													_enableFiltering = _this
															.attr("enableFiltering");
													_this
															.multiselect({
																maxHeight : 300,
																buttonWidth : _buttonWidth,
																selectedClass : _selectedClass,
																includeSelectAllOption : (_includeSelectAllOption != null && _includeSelectAllOption != undefined) ? true
																		: false,
																enableFiltering : (_enableFiltering != null && _enableFiltering != undefined) ? true
																		: false,
																selectAllText : _selectAllText,
																onSelectAll : function() {
																	if (_onSelectAll != null
																			&& _onSelectAll != undefined) {
																		eval(_onSelectAll
																				+ "()");
																	}
																}
															});
												});
							}
						},
						selectItem : function(options) {
							if (options.data != null
									&& options.data != undefined) {
								var datas = [];
								if (options.data != ""
										&& options.data.toString().indexOf(",") >= 0) {
									datas = options.data.split(",");
								} else {
									datas.push(options.data);
								}
								options.obj.multiselect('select', datas);
							}
						},
						getObj : function(id) {
							return $("#" + id);
						},
						showToast : function(options) {
							var defaults = {
								title : "温馨提示",// 标题
								msg : "",// 消息
								shortCutFunction : "info",// 色调
								closeButton : true,// 显示关闭按钮
								debug : false,//
								positionClass : "toast-top-center",// 位置
								showDuration : "1000",// 显示用时
								hideDuration : "1000",// 小时用时
								timeOut : "5000",// 自动消失时间
								extendedTimeOut : "1000",
								showEasing : "swing",
								hideEasing : "linear",
								showMethod : "slideDown",// fadeIn，show
								hideMethod : "fadeOut",
								onclick : null
							// 点击事件
							}
							var opts = $.extend(defaults, options);
							toastr.options = opts;
							var $toast = toastr[opts.shortCutFunction](
									opts.msg, opts.title);
							$toastlast = $toast;
						},
						clearlasttoast : function() {
							toastr.clear($toastlast);
						},
						cleartoasts : function() {
							toastr.clear();
						}
					});
	$btools = new BootTools();
	$(document).ready(
			function() {
				$btools.loadSelectData();// 加载所有下拉选择框的数据
				$btools.rendBootselect();
				if ($('[data-toggle="tooltip"]').length > 0) {
					$('[data-toggle="tooltip"]').tooltip();
				}
				$(".fn-btn").click(
						function(e) {// 通用
							e.preventDefault();
							var _this = $(this);
							var fun = _this.attr("btn-fn");
							var _options = _this.attr("options");
							var options = {};
							try {
								if (_options != null && _options != undefined) {
									options = eval('(' + _options + ')');
								}
							} catch (e) {
							}
							if (fun != null && fun != undefined) {
								if (fun == "reset") {// 点击查询按钮
									var _dataformId = _this.attr("dataform");
									var _form = _this.closest("form")[0];
									if (_dataformId != null
											&& _dataformId != undefined
											&& _dataformId != "") {
										_form = document
												.getElementById(_dataformId);
									}
									if (_form != null && _form != undefined) {
										// 重置表单(此方法必须在表单存在id的情况下才有效)
										// document.getElementById(_form.attr("id")).reset();
										_form.reset();// 重置表单（通过调用
										// DOM
										// 中的reset方法来重置表单。）
									}
								} else if (fun == "openUrlInModal") {// 打开模态框
									$btools.openUrlInModal(options);
								}
							}
						});
			});
})(jQuery);